<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends Frontend_Controller
{

	public function __construct()
	{
		parent::__construct();

		$this->template->set_layout('listing_layout');
		//--PARTIALS--//
		$this->template->set_partial('header', 'header/header');
        $this->template->set_partial('footer', 'footer/footer');
		$this->load->model(array('browse/browse_model','term_model'));
		$this->load->library('pagination');
		$this->load->model('gallery_model');
		$this->load->model('category_model');
		$this->load->model('browse_model');
		$this->load->library('pagination');
	}


	function search(){
		$key = $this->input->post('key');
		$locc = $this->input->post('city_id');


		$locaname = $this->input->post('location');

		/*
		 $this->session->set_flashdata('key',$key);
		$this->session->set_flashdata('locID',$locc);
		$this->session->set_flashdata('loc',$locaname);
		//	$this->session->set_flashdata('post', $data['post']['details']);
		*/	write_session('key',$key);
		write_session('locID',$locc);
		write_session('loc',$locaname);
		//var_dump($data['post']['details']);die();
		redirect('business/'.seoUrl($key).'/'.seoUrl($locaname));

	}


	function business(){

		$data['key'] = get_session('key');
		$data['loc'] = get_session('loc');
		$data['locID'] = get_session('locID');

		
		
		$key = $data['key'];
		$loc = $data['loc'];
		$locID = $data['locID'];
		$data['page'] = "SEARCH";

		
		/*START : CREATE PAGINATION (RAHUL) 18/06/14*/
		$limit = 10;
		
		if($this->input->get('page') && $this->input->get('page') > 0){
			$offset = $this->input->get('page');
		}else{
			$offset = 0;
		}
		/*END : CREATE PAGINATION (RAHUL) 18/06/14*/
		
		/*START : CREATE PAGINATION (RAHUL) 18/06/14*/
		$seg_cat = $this->uri->segment(2);
		
		$Businsql = "Select count(*)as total from post where title like '%$key%' and status = 51 ";
		
		$query = $this->db->query($Businsql);
			
			if($query){
				$result = $query->row();
				
			}else{
					
				$result = FALSE;
			}
		
			$config['total_rows'] = $result->total;
			
		
		if($locID == 0){
			$config['base_url'] = base_url() . 'business/' . $seg_cat;
			$cur =base_url()."business/'.$seg_cat";
		}if($locID != 0){
			$config['base_url'] = base_url() . 'business/' . $seg_cat.'/'.$loc;
			$cur =base_url()."business/'.$seg_cat.'/'.$loc";
			
		}
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<ul class="pagination">';
			$config['full_tag_close'] = '</ul><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			$this->pagination->initialize($config);
			/*END : CREATE PAGINATION (RAHUL) 18/06/14*/

		$sql = "SELECT P.* FROM post AS P
		LEFT JOIN term AS T ON P.id = T.post_id WHERE P.title like '%$key%' and T.value= '$locID' and T.term = 'city' limit $offset,$limit
		";
			
		if($locID == 0){
			$sql = "select * from post where title like '%$key%' and status = 51 limit $offset,$limit";
		}

		//print($sql);die();

		//$sql = "select * from post where title like '%$key%' and status = 51 limit 10";

		$query = $this->db->query($sql);
		if($query){
			$result = $query->result();

		}

		$data['post']['details'] = $result;
		//var_dump($result);die();



		$sql ="SELECT * FROM gallery
		WHERE shows=2 and term='banner' limit 3";
		//	print $sql;die();

		$query = $this->db->query($sql);
		if($query){
			$result = $query->result();

		}else{

			$result = FALSE;
		}

		$data['bannerInner'] = $result;


		//--WIDGET--//
		$data['adsense'] = $this->setting('adsense_pub_id');
		$data['widget_recent_listing'] = TRUE;
		$data['widget_popular_listing'] = TRUE;

		//--META DATA---//
		$this->template->title("Searching for $key ", setting('web_title'));
		$this->template->set_metadata('author', setting('google_plus'));


		$this->template->build('search',$data);

	}


	function business_search(){

		$search = $_GET['term'];

		$query = $this->db->query("SELECT id,title FROM `post` WHERE `title` LIKE '%$search%' and status=51 ORDER BY title ASC limit 15") or die('Something went wrong');
		$result = $query->result();

		$row_set = array();

		foreach ($result as $row1){
			$row = array(
					'label'	=> stripslashes($row1->title),
					'value'	=> 	(int)$row1->id
			);
			$row_set[] = $row;//build an array
		}

		echo json_encode($row_set);//format the array into json data



	}

	function category()
	{

		$cat = $this->input->post('cat');

		$catID = $this->category_model->getID_by_Name($cat,30);
		//	$catID = $this->input->post('cat_id');
		//var_dump($catID);die();

		$city = $this->input->post('loca_id');
		
		
		$cityname = $this->input->post('loca');
		//$c = $this->location_model->getID_by_Name($cityname,30);
		//var_dump($c);die();
		
		
		
		
		
		
		write_session('cat',$cat);
		write_session('catID',$catID);
		write_session('cityID',$city);
		write_session('cityname',$cityname);
		
		/*$this->session->set_flashdata('cat',$cat);
		 $this->session->set_flashdata('catID',catID);
		$this->session->set_flashdata('cityID',$city);
		$this->session->set_flashdata('cityname',$cityname);
		*/
		//var_dump($data['post']['details']);die();
		redirect('keyword/'.seoUrl($cat).'/'.seoUrl($cityname));

	}


	function keyword(){


		$data['key'] = get_session('cat');
		$data['catID'] = get_session('catID');
		$data['loc'] = get_session('cityname');
		$data['cityID'] = get_session('cityID');
		
		/*START : CREATE PAGINATION (RAHUL) 18/06/14*/
		$limit = 10;
		
		if($this->input->get('page') && $this->input->get('page') > 0){
			$offset = $this->input->get('page');
		}else{
			$offset = 0;
		}
		/*END : CREATE PAGINATION (RAHUL) 18/06/14*/
		
		$data['page'] = "SEARCH";
		$key = $data['key'];
		$catID = $data['catID'];
		$loc = $data['loc'];
		$cityID = $data['cityID'];
		
		if($catID){

			$gSQL='';
			for($i=0; $i < count($catID); $i++){
				$gSQL .= "T.value = ".$catID[$i]->id." OR ";
			}



			//-- REMOVE LAST "OR"---//
			$len=strlen($gSQL);
			$pos=strrpos($gSQL, 'OR');
			$gSQL= substr($gSQL,0,$pos);


			/*START : CREATE PAGINATION (RAHUL) 18/06/14*/
			$seg_cat = $this->uri->segment(2);
			$ctSQL='';
			for($i=0; $i < count($catID); $i++){
				$ctSQL .= "value = ".$catID[$i]->id." OR ";
			}
			
			$len=strlen($ctSQL);
			$pos=strrpos($ctSQL, 'OR');
			$ctSQL= substr($ctSQL,0,$pos);
			
			if($cityID == 0){
			$Csql ="Select count(*)as total from term WHERE ($ctSQL) and (term='category' or term='keyword')";
			}
			if($cityID != 0){
			$Csql = "SELECT P.*, count(P.id) as total from post P
			INNER JOIN term T on P.id = T.post_id
			AND (T.term='category' or T.term='keyword') and ( $gSQL )

			INNER JOIN term T2 on P.id = T2.post_id
			AND (T2.term='country' or T2.term='region' or T2.term='city') and T2.value = $cityID order by (total)";
			}
			$query = $this->db->query($Csql);
			
			if($query){
				$result = $query->row();
				
			}else{
					
				$result = FALSE;
			}
			
			
			$config['total_rows'] = $result->total;
		//	var_dump($config['total_rows']);die();
			
		if($cityID == 0){	
			$config['base_url'] = base_url() . 'keyword/' . $seg_cat;
			$cur =base_url()."keyword/'.$seg_cat";
		}if($cityID != 0){
			$config['base_url'] = base_url() . 'keyword/' . $seg_cat.'/'.$loc;
			$cur =base_url()."keyword/'.$seg_cat";
	}
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<ul class="pagination">';
			$config['full_tag_close'] = '</ul><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			$this->pagination->initialize($config);
			/*END : CREATE PAGINATION (RAHUL) 18/06/14*/
			

			/*START : SEARCH BUSINESS DATA BY KEYWORD WITH CITY (RAHUL) 14-06-2014*/
			$sql = "SELECT P.* from post P
			INNER JOIN term T on P.id = T.post_id
			AND (T.term='category' or T.term='keyword') and ( $gSQL )

			INNER JOIN term T2 on P.id = T2.post_id
			AND (T2.term='country' or T2.term='region' or T2.term='city') and T2.value = $cityID
			Limit $offset,$limit";
			/*END : SEARCH BUSINESS DATA BY KEYWORD WITH CITY (RAHUL) 14-06-2014*/



			/*START : SEARCH BUSINESS DATA BY KEYWORD WITHOUT CITY (RAHUL) 14-06-2014*/
			if($cityID == 0){

				$sql ="SELECT P.* FROM post AS P
				LEFT JOIN term AS T ON P.id  = T.post_id
				WHERE ($gSQL) and (T.term='category' or T.term='keyword') limit $offset,$limit";
			}
			/*END : SEARCH BUSINESS DATA BY KEYWORD WITHOUT CITY (RAHUL) 14-06-2014*/
			
		//		print $sql;die();
			$query = $this->db->query($sql);
			if($query){
				$result = $query->result();

			}else{
					
				$result = FALSE;
			}


			$data['post']['details'] = $result;
			//var_dump($data['post']['details']);die();

		}else{

			$data['post']['details'] = '';
		}

		/*START : CREATE BANNER BY CATEGORY (RAHUL) 17/06/14*/
		if($catID != 0){
			$pSQL='';
			for($i=0; $i < count($catID); $i++){
				$pSQL .= "cat_id = ".$catID[$i]->id." OR ";
			}
			$len=strlen($pSQL);
			$pos=strrpos($pSQL, 'OR');
			$pSQL= substr($pSQL,0,$pos);

			$sql ="SELECT * FROM gallery
			WHERE ($pSQL) and (term='banner') limit 3";
			
			$query = $this->db->query($sql);
			if($query){
				$result = $query->result();
					
			}else{
					
				$result = FALSE;
			}
			$data['bannerInner'] = $result;
		}
	
		if($catID =='0'){

		
		$data['bannerInner'] = NULl;
		}
		
		//	$data['bannerInner'] = $result;
		/*END : CREATE BANNER BY CATEGORY (RAHUL) 17/06/14*/



		

		//--WIDGET--//
		$data['adsense'] = $this->setting('adsense_pub_id');
		$data['widget_recent_listing'] = TRUE;
		$data['widget_popular_listing'] = TRUE;

		//--META DATA---//
		$this->template->title("Searching for $key ", setting('web_title'));
		$this->template->set_metadata('author', setting('google_plus'));


		$this->template->build('search',$data);


	}
	
	
	function zip(){
		
	}

}
